home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / Miracle C Compiler / miricleC compiler.msi / Instal01.cab / _B60F88DAD8DE435EB1CA042E3706F214 < prev    next >
Encoding:
Text File  |  2000-07-25  |  6.4 KB  |  190 lines

  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4.  
  5. void xmath();
  6.  
  7. int roughly(double x, double y)
  8. {
  9.     if(y==0.0)
  10.        return (fabs(x) < 1.0e-6);
  11.     else
  12.        return (fabs((y-x)/y) < 1.0e-6);
  13. }
  14.  
  15. int main()
  16. {
  17.     xmath();
  18. }
  19.  
  20. void xmath()
  21. {
  22.     double roottwo=    1.4142135623730950488016887242097;    // --- from Windows calculator
  23.     double my_pi=    3.1415926535897932384626433832795;
  24.     double my_e=    2.71828182845904523536028747135266;
  25.  
  26.     printf("==> starting xmath <==\n");
  27.  
  28.  
  29.     // --- test the ZMATH functions
  30.     // ----------------------------
  31.  
  32.     // --- test sqrt
  33.     assert(roughly(sqrt(0.0),0.0));
  34.     assert(roughly(sqrt(1.0),1.0));
  35.     assert(roughly(sqrt(-1.0),0.0));
  36.  
  37.     assert(roughly(sqrt(25.0),5.0));
  38.     assert(roughly(sqrt(-25.0),0.0));
  39.  
  40.     assert(roughly(sqrt(2.0),roottwo));
  41.     assert(roughly(sqrt(0.5),1.0/roottwo));
  42.     printf("passed sqrt...   ");
  43.  
  44.     // --- test sqrt
  45.     assert(ceil(5.5)==6.0);        assert(ceil(-5.5)==-5.0);
  46.     assert(ceil(6.6)==7.0);        assert(ceil(-6.6)==-6.0);
  47.     assert(ceil(700.7)==701.0);    assert(ceil(-700.7)==-700.0);
  48.  
  49.     assert(floor(5.5)==5.0);    assert(floor(-5.5)==-6.0);
  50.     assert(floor(6.6)==6.0);    assert(floor(-6.6)==-7.0);
  51.     assert(floor(700.7)==700.0);    assert(floor(-700.7)==-701.0);
  52.  
  53.     assert(ceil(2.0)==2.0);        assert(ceil(-2.0)==-2.0);
  54.     assert(floor(2.0)==2.0);    assert(floor(-2.0)==-2.0);
  55.     printf("passed ceil/floor...   ");
  56.  
  57.     // --- test sin, cos, tan
  58.     assert(roughly(sin(0.0),0.0));                // --- test sine -pi to +pi
  59.     assert(roughly(sin(0.25 * my_pi),1.0/roottwo));
  60.     assert(roughly(sin(0.5  * my_pi),1.0));
  61.     assert(roughly(sin(0.75 * my_pi),1.0/roottwo));
  62.     assert(roughly(sin(1.0  * my_pi),0.0));
  63.  
  64.     assert(roughly(sin(-0.25 * my_pi),-1.0/roottwo));
  65.     assert(roughly(sin(-0.5  * my_pi),-1.0));
  66.     assert(roughly(sin(-0.75 * my_pi),-1.0/roottwo));
  67.     assert(roughly(sin(-1.0  * my_pi),0.0));
  68.  
  69.     assert(roughly(cos(0.0),1.0));                // --- test cosine -pi to +pi
  70.     assert(roughly(cos(0.25 * my_pi),1.0/roottwo));
  71.     assert(roughly(cos(0.5  * my_pi),0.0));
  72.     assert(roughly(cos(0.75 * my_pi),-1.0/roottwo));
  73.     assert(roughly(cos(1.0  * my_pi),-1.0));
  74.  
  75.     assert(roughly(cos(-0.25 * my_pi),1.0/roottwo));
  76.     assert(roughly(cos(-0.5  * my_pi),0.0));
  77.     assert(roughly(cos(-0.75 * my_pi),-1.0/roottwo));
  78.     assert(roughly(cos(-1.0  * my_pi),-1.0));
  79.  
  80.     assert(roughly(tan(0.0),0.0));                // --- test tangent -pi to +pi
  81.     assert(roughly(tan(0.25 * my_pi),1.0));
  82.     assert(roughly(tan(0.75 * my_pi),-1.0));
  83.     assert(roughly(tan(1.0  * my_pi),0.0));
  84.  
  85.     assert(roughly(tan(-0.25 * my_pi),-1.0));
  86.     assert(roughly(tan(-0.75 * my_pi),1.0));
  87.     assert(roughly(tan(-1.0  * my_pi),0.0));
  88.     printf("passed sin/cos/tan...\n");
  89.  
  90.     // --- log, log10
  91.     assert(roughly(log(my_e),1.0));                // --- test log and log10
  92.     assert(roughly(log(1.0),0.0));
  93.     assert(roughly(log(my_e + my_e), 1.0 + log(2.0)));
  94.     assert(roughly(log(my_e * my_e * 2.0), 2.0 + log(2.0)));
  95.  
  96.     assert(roughly(log10(10.0),1.0));
  97.     assert(roughly(log10(1.0),0.0));
  98.     assert(roughly(log10(10.0 + 10.0), 1.0 + log10(2.0)));
  99.     assert(roughly(log10(10.0 * 10.0 * 2.0), 2.0 + log10(2.0)));
  100.     printf("passed log/log10...   ");
  101.  
  102.     // --- exp, pow
  103.     assert(roughly(exp(0), 1.0));                // --- test exp and pow
  104.     assert(roughly(exp(1.0), my_e));
  105.     assert(roughly(exp(2.0), my_e * my_e));
  106.     assert(roughly(exp(-2.0),1.0/(my_e * my_e)));
  107.     assert(roughly(exp(-3.0),1.0/(my_e * my_e * my_e)));
  108.     assert(roughly(exp(log(my_pi)),my_pi));
  109.  
  110.     assert(roughly(pow(2.0,2.0), 4.0));
  111.     assert(roughly(pow(-3.0,3.0), -27.0));
  112.     assert(roughly(pow(2.0,0.5), roottwo));
  113.     assert(roughly(pow(4.0,0.25), roottwo));
  114.     assert(roughly(pow(2.0,-4.0), 0.0625));
  115.     assert(roughly(pow(1.5,2.0), 2.25));
  116.     assert(roughly(pow(-1.5,3.0), -3.375));
  117.     printf("passed exp/pow...   ");
  118.  
  119.     // --- atan2
  120.     assert(roughly(atan2(0.0,0.0), 0.0));            // --- test atan2 ... we define atan2(0,0)=0
  121.     assert(roughly(atan2(0.0,1.0), 0.0));            // --- y=0, x=1
  122.     assert(roughly(atan2(0.0,-1.0), my_pi));        // --- y=0, x=-1
  123.  
  124.     assert(roughly(atan2(1.0,0.0), my_pi/2.0));
  125.     assert(roughly(atan2(1.0,-1.0), my_pi*3.0/4.0));
  126.     assert(roughly(atan2(1.0,1.0), my_pi/4.0));
  127.  
  128.     assert(roughly(atan2(-1.0,0.0), -my_pi/2.0));
  129.     assert(roughly(atan2(-1.0,-1.0), -my_pi*3.0/4.0));
  130.     assert(roughly(atan2(-1.0,1.0), -my_pi/4.0));
  131.     printf("passed atan2...\n");
  132.  
  133.  
  134.     // --- test the MATHFUNC functions
  135.     // -------------------------------
  136.  
  137.     // --- fabs
  138.     assert(roughly(fabs(0.0), 0.0));            // --- test fabs
  139.     assert(roughly(fabs(my_pi), my_pi));
  140.     assert(roughly(fabs(-my_pi), my_pi));
  141.     printf("passed fabs...   ");
  142.  
  143.     // --- sinh/cosh/tanh
  144.     assert(roughly(sinh(0.0), 0.0));            // --- test sinh
  145.     assert(roughly(sinh(1.0), (my_e - 1.0/my_e)/2.0));    // --- exp(1)=my_e
  146.     assert(roughly(sinh(-1.0), ((1.0/my_e) - my_e)/2.0));    // --- exp(-1)=1/my_e
  147.  
  148.     assert(roughly(cosh(0.0), 1.0));            // --- test cosh
  149.     assert(roughly(cosh(1.0), (my_e + 1.0/my_e)/2.0));    // --- exp(1)=my_e
  150.     assert(roughly(cosh(-1.0), (my_e + 1.0/my_e)/2.0));
  151.  
  152.     assert(roughly(tanh(0.0), 0.0));            // --- test tanh
  153.     assert(roughly(tanh(1.0), (my_e*my_e - 1.0) / (my_e*my_e + 1.0)));
  154.     assert(roughly(tanh(-1.0), -(my_e*my_e - 1.0) / (my_e*my_e + 1.0)));
  155.     printf("passed sinh/cosh/tanh...   ");
  156.  
  157.     // --- asinh/acosh/atanh
  158.     assert(roughly(asinh(0.0), 0.0));            // --- test asinh
  159.     assert(roughly(asinh(1.0), log(1.0 + roottwo)));    // --- because sinh(log(1+roottwo))==1
  160.     assert(roughly(asinh(-1.0), -log(roottwo + 1.0)));    // --- because sinh(-log(roottwo+1))==-1
  161.  
  162.     assert(roughly(acosh(1.0), 0.0));            // --- test acosh
  163.     assert(roughly(acosh(roottwo), log(roottwo+1)));    // --- because cosh(log(1+roottwo))=roottwo
  164.  
  165.     assert(roughly(atanh(0.0), 0.0));            // --- test atanh
  166.     assert(roughly(atanh(0.5), 0.5 * log(3.0)));        // --- because tanh(0.5 * log(3))=0.5
  167.     printf("passed asinh/acosh/atanh...\n");
  168.  
  169.     // --- asin/acos/atan
  170.     assert(roughly(asin(0.0), 0.0));            // --- test asin
  171.     assert(roughly(asin(1.0), my_pi / 2.0));
  172.     assert(roughly(asin(-1.0), -my_pi / 2.0));
  173.     assert(roughly(asin(1.0/roottwo), my_pi / 4.0));
  174.     assert(roughly(asin(-1.0/roottwo), -my_pi / 4.0));
  175.  
  176.     assert(roughly(acos(1.0), 0.0));            // --- test acos
  177.     assert(roughly(acos(0.0), my_pi / 2.0));
  178.     assert(roughly(acos(-1.0), my_pi));
  179.     assert(roughly(acos(1.0/roottwo), my_pi / 4.0));
  180.     assert(roughly(acos(-1.0/roottwo), 3.0 * my_pi / 4.0));
  181.  
  182.     assert(roughly(atan(0.0), 0.0));            // --- test atan
  183.     assert(roughly(atan(1.0), my_pi / 4.0));
  184.     assert(roughly(atan(-1.0), -my_pi / 4.0));
  185.     printf("passed asin/acos/atan...\n");
  186.  
  187.     printf("==> finished xmath <==\n");
  188.     return;
  189. }
  190.